/**
 * 
 */
package cw.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cw.entity.Group;
import cw.page.PageUtil;
import cw.service.GroupService;
import cw.util.Log;

/**
 * @author Administrator
 *
 */
@Controller
@RequestMapping("group")
public class GroupController {
	
	private static Logger logger = Logger.getLogger(GroupController.class);

	@Resource
	private GroupService groupServiceImpl;
	
	
	/**
     * 默认页面
     */
    @RequestMapping(method = {RequestMethod.POST,RequestMethod.GET})
    public String list(HttpServletRequest request){
        return "showGroup";
    }
	
	@RequestMapping(value="json",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public Map<String,Object> showGroup(HttpServletRequest request) {
	  
	  		    
      String groupName = request.getParameter("groupName");
      String description =request.getParameter("description");
      String id = request.getParameter("id1");
      
      Integer total = groupServiceImpl.getTotal(groupName, description,id);
      
      Integer pageSize = Integer.parseInt(request.getParameter("rows"));
      
      
      Integer page = Integer.parseInt(request.getParameter("page"));
      
      PageUtil pageUtil = new PageUtil(pageSize,total,page);
      
      List<Group> group = groupServiceImpl.queryGroup(groupName, description,id, pageUtil.getFromIndex(), pageUtil.getToIndex());
      
      Map<String,Object> map = new HashMap<>();
      
      map.put("total", total);
      map.put("rows", group);
       
      return map;
   }
	
	/**
     * 增加跳转
     */
    @RequestMapping(value = "create", method = RequestMethod.GET)
    public String createForm(Model model) {
        model.addAttribute("group", new Group());
        model.addAttribute("action", "create");
        return "groupForm";
    }
    
    @RequestMapping(value = "create", method = RequestMethod.POST)
    @ResponseBody
    public String create(@Valid Group group, Model model) { 	  	
        groupServiceImpl.insertSelective(group);
        Log.write("增加成功！"+new Date().toString()+"---"+group.toString());
        return "success";
    }

    /**
     * 修改跳转
     */
    @RequestMapping(value = "update/{id}", method = RequestMethod.GET)
    public String updateForm(@PathVariable("id")Integer id, Model model) {
        model.addAttribute("group", groupServiceImpl.selectByPrimaryKey(id));
        model.addAttribute("action", "update");
        return "groupForm";
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public String update(@Valid Group group, Model model) {
    	logger.info(group); 	
    	Log.write("更新成功!"+new Date().toString()+"---"+group.toString());
    	groupServiceImpl.updateByPrimaryKeySelective(group);
        return "success";
    }

    @RequestMapping(value = "delete/{id}")
    @ResponseBody
    public String delete(@PathVariable("id")Integer id) {
    	System.out.println(id);
    	int num = groupServiceImpl.deleteByPrimaryKey(id);
    	if(num>0){
    		return "success";
    	}
    	Log.write("删除成功!"+new Date().toString()+"---"+id);
        return "fail";
    }
	
}
